package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.mapper.*;
import com.dongdong.pojo.*;
import com.dongdong.service.OrderService;
import com.dongdong.vo.OrderVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@org.springframework.stereotype.Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private TbAddressMapper tbAddressMapper;
    @Resource
    private TbAreasMapper tbAreasMapper;
    @Resource
    private TbCitiesMapper tbCitiesMapper;
    @Resource
    private TbProvincesMapper tbProvincesMapper;
    @Resource
    private TbOrderItemMapper orderItemMapper;
    @Resource
    private TbOrderMapper tbOrderMapper;
    @Resource
    private TbPayLogMapper payLogMapper;
    @Override
    public List<TbAddress> selectOne(String username) {
        TbAddressExample te = new TbAddressExample();
        te.createCriteria().andUserIdEqualTo(username);
        return tbAddressMapper.selectByExample(te);
    }

    @Override
    public List<TbProvinces> selectTwo() {
        return tbProvincesMapper.selectByExample(null);
    }
    @Override
    public List<TbCities> selectThree(){
        return tbCitiesMapper.selectByExample(null);
    }
    @Override
    public List<TbAreas> selectFour(){
        return tbAreasMapper.selectByExample(null);
    }

    @Override
    public List<TbAddress> adressGetInfo(String username ) {
        TbAddressExample te = new TbAddressExample();
        te.createCriteria().andUserIdEqualTo(username);
        return tbAddressMapper.selectByExample(te);
    }

    @Override
    public int deleteById(Long id) {
        return tbAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TbProvinces> selectProvinces() {
        return tbProvincesMapper.selectByExample(null);
    }

    @Override
    public List<TbCities> getClist(String province) {
        TbCitiesExample te = new TbCitiesExample();
        te.createCriteria().andProvinceidEqualTo(province);
        return tbCitiesMapper.selectByExample(te);
    }

    @Override
    public List<TbAreas> getAlareasList(String areaid) {
        TbAreasExample te = new TbAreasExample();
        te.createCriteria().andAreaidEqualTo(areaid);
        return tbAreasMapper.selectByExample(te);
    }

    @Override
    public List<TbCities> getCityByPid(String provinceid) {
        TbCitiesExample te = new TbCitiesExample();
        te.createCriteria().andProvinceidEqualTo(provinceid);
        return tbCitiesMapper.selectByExample(te);
    }

    @Override
    public List<TbAreas> getAreasCityid(String cityid) {
        TbAreasExample te = new TbAreasExample();
        te.createCriteria().andCityidEqualTo(cityid);
        return tbAreasMapper.selectByExample(te);
    }

    @Override
    public boolean insert(TbAddress tbAddress) {
       int i = tbAddressMapper.insert(tbAddress);
       if (i > 0){
           return true;
       }else {
           return false;
       }
    }

    @Override
    public int deleteByIdS(Long id) {
        return tbAddressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TbProvinces> selectProvincesList() {
        return tbProvincesMapper.selectByExample(null);
    }

    @Override
    public TbAddress getAddressById(Long id) {
        return  tbAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertTbOrder(TbOrder tbOrder) {
        tbOrderMapper.insertSelective(tbOrder);
    }

    @Override
    public void insertOrderItem(TbOrderItem orderItem) {
        orderItemMapper.insertSelective(orderItem);
    }

    @Override
    public void updateInfo(String out_trade_no, String trade_no) {

        tbOrderMapper.updateByOrderTradeNo(out_trade_no,trade_no);
       /* Long ix = Long.valueOf(out_trade_no);
        TbOrder tbOrder = new TbOrder();
        tbOrder.setTradeNo(trade_no);
        tbOrder.setStatus("2");
        TbOrderExample tb = new TbOrderExample();
        tb.createCriteria().andOutTradeNoEqualTo(ix);
        tbOrderMapper.updateByExample(tbOrder,tb);*/
       /* TbOrder tbOrder = tbOrderMapper.selectByTrade(trade_no);
        TbOrderItem tbOrderItem = orderItemMapper.selectByTrade(trade_no);*/
        /*TbOrder tbOrder1 = new TbOrder();
        tbOrder1.setUserId(tbOrder.getUserId());
        tbOrder1.setPayment(tbOrder.getPayment());
*/
    }

    @Override
    public void updateByOrderItem(String out_trade_no, String trade_no) {
      orderItemMapper.updateByOrderItemTradeNo(out_trade_no,trade_no);

        /*TbOrderItem item = new TbOrderItem();
        item.setTradeNo(trade_no);
        TbOrderItemExample itemExample = new TbOrderItemExample();
        itemExample.createCriteria().andOutTradeNoEqualTo(new BigDecimal(out_trade_no).longValue());
        orderItemMapper.updateByExample(item,itemExample);*/
    }

    @Override
    public void insertPayLog(TbPayLog payLog) {
        payLogMapper.insertSelective(payLog);
    }
    @Override
    public void selectOrderByOutTradeNo(String out_trade_no) {
        TbOrderExample tb = new TbOrderExample();
        tb.createCriteria().andOutTradeNoEqualTo(new BigDecimal(out_trade_no).longValue());
        TbOrder to = new TbOrder();
        to.setOutTradeNo(new BigDecimal(out_trade_no).longValue());

    }
    @Override
    public List<OrderVO> findOrderSend(TbUser user) {
        List<OrderVO> voList = new ArrayList<>();
        TbOrderExample tb = new TbOrderExample();
        tb.createCriteria().andUserIdEqualTo(user.getUsername()).andStatusEqualTo("2");
        List<TbOrder> orderList = tbOrderMapper.selectByExample(tb);
        for (TbOrder tbOrder : orderList) {
            Long orderId = tbOrder.getOrderId();
            TbOrderItemExample itemExample = new TbOrderItemExample();
            TbOrderItemExample.Criteria criteria1 = itemExample.createCriteria();
            criteria1.andOrderIdEqualTo(orderId);
            List<TbOrderItem> orderItemList = orderItemMapper.selectByExample(itemExample);
            OrderVO vo = new OrderVO();
            vo.setOrder(tbOrder);
            vo.setOrderItemList(orderItemList);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public void insertOrderAndItem(String wiDout_trade_no, String wiDsubject, String wiDtotal_amount, long l1, long l2) {
        TbUser user =  (TbUser) SecurityUtils.getSubject().getPrincipal();
        //增加TbOrder表
        TbOrder order = new TbOrder();
        order.setOutTradeNo( Long.valueOf(wiDout_trade_no));
        order.setPayment(new BigDecimal(wiDtotal_amount));
        order.setSellerId(user.getUsername());
        order.setCreateTime(new Date());
        order.setEndTime(new Date());
        order.setStatus("1");
        tbOrderMapper.insertSelective(order);
        TbOrderItem item = new TbOrderItem();
        item.setOutTradeNo(Long.parseLong(wiDout_trade_no));
        item.setTotalFee(new BigDecimal(wiDtotal_amount));
        item.setTitle(wiDsubject);
        item.setSellerId(user.getUsername());
        orderItemMapper.insertSelective(item);

    }

    @Override
    public void updateByOutTrdateNo(String out_trade_no, String trade_no) {
        tbOrderMapper.updateByOrderTradeNo(out_trade_no,trade_no);
        orderItemMapper.updateByOrderItemTradeNo(out_trade_no,trade_no);
    }
}
